//
//  QGVisitorView.swift
//  斯威夫特微博
//
//  Created by youyoumacmini3 on 2017/7/5.
//  Copyright © 2017年 youyoumacmini3. All rights reserved.
//

import UIKit


/// 访客视图
class QGVisitorView: UIView {

    
    // 注册按钮
    lazy var registBtn:UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), highlightedColor: #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), backgroundImageName: "common_button_white_disable")
    // 登录按钮
    lazy var loginBtn:UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1), highlightedColor: #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1), backgroundImageName: "common_button_white_disable")
    
    
    
    /// 访客视图的信息字典  [imageName / message]
    /// 如果是首页 imageName == ""
    var visitorInfo:[String: String]? {
        
        didSet {
         
            //1. 取字典信息
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else { return  }
            
            // 2. 设置消息
            tipLabel.text = message
            
            // 3. 设置图像
            if imageName == "" {
                startAnimation()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            
            //其他控制器访客视图不需要显示小房子/遮罩视图
            houseView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    
    
    //MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    /// 旋转图标动画 (首页)
    fileprivate func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        
        //如果iconView被释放，动画会一起被销毁
        //在设置连续播放的动画个、非常有用
        anim.isRemovedOnCompletion = false//完成后不删除
        //将动画添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    
    
    //MARK: - 私有控件
    //懒加载属性只有调用 UIKit 控件指定构造函数，其他都需要使用类型
    // 图像视图
    fileprivate lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩图像
    fileprivate lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    // 小房子
    fileprivate lazy var houseView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    
    // 提示标签
    fileprivate lazy var tipLabel:UILabel = UILabel.cz_label(withText: "关注去are是通过的双方各地方广东省个人股的刀哥的双方各吧", fontSize: 14, color: UIColor.darkGray)
    
  

}


// MARK: - 设置界面
extension QGVisitorView {
    
    fileprivate func setUI() {
        
        //开发的时候 能用颜色就不要用图片
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        /// 1. 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(tipLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        //文本居中
        tipLabel.textAlignment = .center
        
        /// 2. 取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        /// 3. 自动布局
        /*
            核心公式：view1.attr1 = view2.attr2 * multiplier + constant
              item          视图
              attribute     约束属性
              relatedBy     约束关系
              toItem        参照视图
              multiplier    乘积
              constant      约束数值
         */
        
        
        let margin: CGFloat = 20
        // 1> 图像视图
        
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
     
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))
        
        // 3> 提示标签
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))

        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
      
        
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: margin))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: registBtn, attribute: .width, multiplier: 1.0, constant: 0))
        
        /*
 
         VFL 可视化格式语言
         H    水平方向
         V    垂直方向
         |    边界
         []   包含控件的名称字符串，对应关系在 views 字典中定义
         ()   定义控件的宽高，可以再metrics 中指定
        */
        
        // 6> 遮罩图像
        // views : 定义VFL 中的控件名称和实际名称的映射关系
        // metrics : 定义VFL 中()指定常数映射关系
        // ！！！注意："H:|-0-[maskIconView]-0-|" 之间不要留空格/多符号 之类的 可能崩溃，也可能布局错乱
        let viewDic = ["maskIconView": maskIconView,"registBtn": registBtn] as [String : Any]
        let metrics = ["spacing": 20]
        
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDic))
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconView]-(spacing)-[registBtn]",
            options: [],
            metrics: metrics,
            views: viewDic))
        

        
    }
}




